﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ServiceModel;
using System.IO.IsolatedStorage;
using System.Text.RegularExpressions;
using QuickTasks.FileStoreService;
using System.Globalization;
using System.Windows.Media.Imaging;
using QuickTasks.FileStoreService;
using QuickTasks;


namespace QuickTasks
{
    public partial class Feedback : PhoneApplicationPage
    {
        

        static readonly string[] Topics = { "SMS", "EMAILS",  };

        public Feedback()
        {
            InitializeComponent();
            DataContext = Topics;
        }

       
        private void ApplicationBarIconSendEmailButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (TopicListPicker.SelectedIndex != -1)
                {
                    if (txtboxFeedback.Text != "")
                    {
                        tblkMessageValidation.Visibility = Visibility.Collapsed;
                        string body = txtboxFeedback.Text + Exceptions.BuildMailMessage();
                        Service1Client service = new Service1Client();
                        string fromAddress = tbxMailId.Text;
                        string Toaddress = "";
                        if (fromAddress != "")
                        {
                            if (Regex.IsMatch(fromAddress, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
                            {
                                tblkMailIdValidation.Text = "(optional)";
                                tblkMailIdValidation.Foreground = new SolidColorBrush(Colors.White);
                                service.SendMailCompleted += new EventHandler<SendMailCompletedEventArgs>(service_SendMailCompleted);
                            }
                            else
                            {
                                tblkMailIdValidation.Text = "Please Enter Correct Mail Id";
                                tblkMailIdValidation.Foreground = new SolidColorBrush(Colors.Red);
                            }
                        }
                        else
                        {
                            fromAddress ="";
                            service.SendMailAsync(fromAddress, Toaddress, TopicListPicker.SelectedItem.ToString(), body);
                            service.SendMailCompleted += new EventHandler<SendMailCompletedEventArgs>(service_SendMailCompleted);
                        }
                    }
                    else
                        tblkMessageValidation.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex)
            {
                string excepmess = "Exception in ApplicationBarIconButton_Click Method In Feedback file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
                Exceptions.SaveExceptionInLocalStorage(excepmess);
            }
        }

        void service_SendMailCompleted(object sender, SendMailCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("Feedback sent successfully");
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
            else if (e.Error is FaultException)
            {
                MessageBox.Show("Failed to send feedback, try again later.");
                FaultException fault = e.Error as FaultException;
                Exceptions.SaveExceptionInLocalStorage(e.Error.Message);
            }
        }

      


    }
}